//
//  UserDefaults.m
//  UserDefaults
//
//  Created by yintr on 15/9/4.
//  Copyright (c) 2015年 asput. All rights reserved.
//

#import "UserDefaults.h"

@implementation UserDefaults

+(id)readUserDefaultObjectValueForKey:(NSString*)aKey
{
    if (aKey) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [defaults objectForKey:aKey];
        return [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    else {
        return nil;
    }
}

+(void)writeUserDefaultObjectValue:(NSObject*)aValue withKey:(NSString*)aKey
{
    if (!aValue || !aKey)
    {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSObject *objc = aValue ; // set value
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objc];
    [defaults setObject:data forKey:aKey];
    [defaults synchronize];
}

+(void)clearUserDefaultWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}


+(void)createList{
 
    NSString *cacheDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filename = [cacheDirectory stringByAppendingPathComponent:@"subscriptionHouse.plist"];

    NSFileManager * fileManager = [NSFileManager defaultManager];
    //判断文件是否存在
//    if (![[NSFileManager defaultManager] fileExistsAtPath:filename]) {//如果文件不存在则创建
        //更改到待操作的目录下
        [fileManager changeCurrentDirectoryPath:[filename stringByExpandingTildeInPath]];
        //创建文件fileName文件名称，初始化 contents文件的内容，attributes文件的属性，初始为nil
        [fileManager createFileAtPath:filename contents:nil attributes:nil];
        //NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        
        //创建一个dic，写到plist文件里
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"House",@"house",nil];
        [dic writeToFile:filename atomically:YES];
//  }
 
}



+(void)writeUserDefaultHouseObjectValue:(NSObject*)aValue withKey:(NSString*)aKey
{
    NSString *cacheDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filename = [cacheDirectory stringByAppendingPathComponent:@"subscriptionHouse.plist"];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:filename]) {//如果文件不存在则创建
        if (!aKey)
        {
            return;
        }
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
        
        NSObject *objc = aValue ; // set value
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objc];
        [tempDic setObject:data forKey:aKey];
        
        [tempDic writeToFile:filename atomically:YES];
    }
}


+(id)readUserDefaultHouseObjectValueForKey:(NSString*)aKey
{
    NSString *cacheDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filename = [cacheDirectory stringByAppendingPathComponent:@"subscriptionHouse.plist"];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:filename]) {//如果文件不存在则创建
        if (!aKey)
        {
            return nil;
        }
//        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
        
//        for (NSMutableDictionary *temp in tempDic) {
//            
//        }
//        NSObject *objc = aValue ; // set value
//        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objc];
//        [tempDic setObject:data forKey:aKey];
//        
//        [tempDic writeToFile:filename atomically:YES];
    }
    
    if (aKey) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [defaults objectForKey:aKey];
        return [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    else {
        return nil;
    }
}
+ (void)writeToPlist:(NSObject *)aValue withKey:(NSString *)aKey {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",aKey]];   //获取路径
    
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];  //读取数据
    NSLog(@"dic2 is:%@",dic2);
    
    //创建一个dic，写到plist文件里
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:aValue,aKey,nil]; //写入数据
    [dic writeToFile:filename atomically:YES];

}

+ (id)readFromPlist:(NSString *)aKey {
    NSMutableArray *resultData;
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",aKey]];
    
    //读文件
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
    NSLog(@"dic is:%@",dic2);
    if(dic2 == nil)
    {
        //1. 创建一个plist文件
        NSFileManager* fm = [NSFileManager defaultManager];
        [fm createFileAtPath:filename contents:nil attributes:nil];
//        return nil;
        resultData=[dic2 objectForKey:aKey];
        if([dic2 count] > 0)
        {
            return resultData;
        }
        else
        {
            return nil;
        }
    }
    else
    {
        resultData=[dic2 objectForKey:aKey];
        if([dic2 count] > 0)
        {
            return resultData;
        }
        else
        {
            return nil;
        }
    }

}






@end









